﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace twilters
{
    public class TwitterClientDummy : TwitterClient
    {
        List<Tweet> tweets = new List<Tweet>();
        List<Filter> filters = new List<Filter>();
        private bool logged = true;
        private String currentUser;
         
        public TwitterClientDummy(bool logged) : this()
        {
            this.logged = logged;
        }

        public TwitterClientDummy()
        {
            currentUser = "dummy";
        }

        public String CurrentUserName()
        {
            return currentUser;
        }

        private void generateTweets()
        {
            Tweet t1 = new Tweet("sigmar", "I like nuts and carrots");
            t1.CreateDate = DateTime.Now;

            Tweet t2 = new Tweet("indrek", "Woohooo, I found out that I am not so stupid as I thought before");
            t2.CreateDate = DateTime.Now;

            Tweet t3 = new Tweet("rene", "Macbook Air is the best notebook in tha world");
            t3.CreateDate = DateTime.Now;

            tweets.Add(t1);
            tweets.Add(t2);
            tweets.Add(t3);


            Filter f1 = new Filter("Filter 1", 1);
            Filter f2 = new Filter("Filter 2", 2);
            Filter f3 = new Filter("Filter 3", 3);

            filters.Add(f1);
            filters.Add(f2);
            filters.Add(f3);

        }

        public void loginAndInitialize(String username, String password)
        {
            this.logged = true;
        }

        public void logout()
        {
            this.logged = false;
        }

        public bool isLogged()
        {
            return logged;
        }

        public List<Tweet> findLatestTweets(TweetType type)
        {
            for (int i = 0; i <= 200; i++)
            {
                String username = RandomString(10, true);
                String text = RandomText();
                Tweet newTweet = new Tweet(username, text);
                newTweet.CreateDate = DateTime.Now;
                tweets.Add(newTweet);
            }

            tweets.Sort(delegate(Tweet t1, Tweet t2) { return -t1.CreateDate.CompareTo(t2.CreateDate); });

            return tweets;
        }

        public List<Filter> getFilters()
        {
            return filters;
        }

        public void postNewTweet(String message)
        {
            Tweet newTweet = new Tweet("dummy", message);
            newTweet.CreateDate = DateTime.Now;
            tweets.Add(newTweet);
        }

        public void postNewTweet(String tweetMessage, String inReplyToID)
        {
        }

        public void removeTweet(String tweetId)
        {
        }

        public void sendDirectMessage(String userID, String text)
        {
        }

        public String CurrentUsername()
        {
            return currentUser;
        }

        public static string RandomText()
        {
            StringBuilder text = new StringBuilder();
            for (int i = 0; i <= 20; i++)
            {
                text.Append(RandomString(10, true) + " ");
            }
            return text.ToString().Trim();
        }

        public static string RandomString(int size, bool lowerCase)
        {
            StringBuilder RandStr = new StringBuilder(size);
            Random r = new Random();
            // Ascii start position (65 = A / 97 = a)
            int Start = (lowerCase) ? 97 : 65;

            // Add random chars
            for (int i = 0; i < size; i++)
                RandStr.Append((char)(26 * r.NextDouble() + Start));

            return RandStr.ToString();
        }

        public void Close()
        {
        }
    }
}
